package com.baizhi.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.entity.Guru;
import com.baizhi.service.GuruService;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Lenovo on 2019/6/17.
 */
@Controller
@RequestMapping("guru")
public class GuruController {

    @Autowired
    private GuruService guruService;

    //分页查询上师
    @RequestMapping("/fenye")
    public @ResponseBody
    Map fenye(int page, int rows, String guruName) throws InterruptedException {
        Thread.sleep(2000);//睡三喵钟
        return guruService.fenye(page, rows, guruName);
    }

    //添加上师
    @RequestMapping("/add")
    public @ResponseBody
    Map add(Guru guru) {
        Map map = new HashMap();
        try {
            guruService.add(guru);
            map.put("addok", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("addok", false);
        }
        return map;
    }


    //修改
    @RequestMapping("/update")
    public @ResponseBody
    Map update(Guru guru) {
        Map map = new HashMap();
        try {
            guruService.update(guru);
            map.put("updateok", true);
        } catch (Exception e) {
            map.put("updateok", false);
            e.printStackTrace();
        }
        return map;
    }


    //删除
    @RequestMapping("/delete")
    public @ResponseBody
    Map delete(@RequestParam("ids[]") int[] ids) {
        Map map = new HashMap();
        try {
            System.out.println("hhhhhhhhhhh哈哈哈哈哈哈哈");
            guruService.delete(ids);
            map.put("deleteok", true);
        } catch (Exception e) {
            map.put("deleteok", false);
            e.printStackTrace();
        }
        return map;
    }


    //文件下载(Excel)(导出)
    @RequestMapping("/xiaZai")
    public void xiaZai(HttpServletResponse response) throws Exception {
        List<Guru> list = guruService.selectAll();
        //创建一个文件对象
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("上师表的所有数据", "hunaishuang"), Guru.class, list);
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("guru", "utf-8"));
        response.setContentType("application/vnd.ms-excel");
        workbook.write(new FileOutputStream(new File("D://easyguru.xls")));
    }



    //文件上传
    @RequestMapping("/shangChuan")
    public @ResponseBody Map shangChuan (@RequestParam("file")MultipartFile file ) throws Exception {
        Map map=new HashMap();
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        InputStream inputStream = file.getInputStream();
        List<Guru> list = ExcelImportUtil.importExcel(inputStream, Guru.class, params);
            for (int i=0;i<list.size();i++){
                try {
                    guruService.add(list.get(i));
                    map.put("shangChuanOk",true);
                }catch (Exception e){
                    map.put("shangChuanOk",false);
                    e.printStackTrace();
                }
            }
        return map;
    }
}
